package com.itzz.ssm.contorller;


import com.itzz.ssm.domain.Role;
import com.itzz.ssm.domain.SysUser;
import com.itzz.ssm.service.RoleService;
import com.itzz.ssm.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @Author:liuweibo
 * @Date Create in 2021/8/10 14:44
 * @Description:
 */
@Controller
@RequestMapping("sysuser")
public class SysUserContorller {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private RoleService roleService;
    @RequestMapping("login")
    public String login(SysUser sysUser, HttpSession session){
        System.out.println(sysUser.getUsername()+"....."+sysUser.getPassword());
        /*登录查询用户*/
        SysUser sysuser1= sysUserService.findUserNameAndPassword(sysUser);
        /*判断用户是否登录成功*/
        if(sysuser1!=null){
            //登录成功后存储session
            session.setAttribute("user",sysuser1);
            return "/pages/main";//首页
        }
       return "redirect:/login.jsp";//返回登录
    }
    /*SysUser查询所有用户*/
    @RequestMapping("findUser")
    public String findUser(HttpServletRequest request){
        List<SysUser> sysUserlist = sysUserService.findSysUser();
        request.setAttribute("sysUserlist",sysUserlist);
        System.out.println(sysUserlist);
        return "/pages/user-list";
    }
    /*删除用户*/
    @RequestMapping("delete/{id}")
    public String deleteSysUser(@PathVariable("id") int id){
            sysUserService.deleteSysUserById(id);
        return "redirect:/sysuser/findUser";
    }
    /*添加用户视图*/
    @RequestMapping("addUserView")
    public  String saveSysUser(HttpServletRequest request){
        List<Role> role = roleService.findRole();//查询所有角色
        System.out.println(role);
        request.setAttribute("listrole",role);
        return "/pages/user-add";
    }

    /*添加用户*/
    @RequestMapping("addSysUser")
    public  String addSysUser(SysUser sysUser,Long[] roleId){
        System.out.println(sysUser+"..."+Arrays.toString(roleId));
        return "redirect:/sysuser/findUser";
    }
    /*修改用户视图*/
    @RequestMapping("updateSysUser/{id}")
    public  String updateSysUser(@PathVariable("id") int id,HttpServletRequest request){
        /*根据id查询用户数据*/
        /*绑定一条数据 request*/
        SysUser sysUser = new SysUser();
            sysUser.setUsername("张三");
            request.setAttribute("user",sysUser);
        return "/pages/user-update";
    }
}
